package com.wei.duowan.bbs.ui.activity;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.ui.adapter.BaseDelegateAdapter;
import com.wei.duowan.bbs.ui.adapter.BaseViewHolder;
import com.wei.duowan.bbs.widget.DialogUtils;

import java.util.Arrays;

import butterknife.Bind;

/**
 * 作者：赵若位
 * 时间：2019/10/17 10:53
 * 联系方式：1070138445
 * 功能：账号管理
 */
public class AccountManagerActivity extends BaseActivity
{
    private static final String TAG = AccountManagerActivity.class.getSimpleName();

    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private DelegateAdapter mAdapter;
    private BaseDelegateAdapter mAccountAdapter, mLogoutAdapter;
    /*是否编辑当前帐号*/
    private boolean isEditor = false;

    /*切换账号Dialog*/
    private DialogUtils mAccountDialog;
    /*退出登录Dialog*/
    private DialogUtils mLogoutDialog;


    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return R.layout.activity_account_manager;
    }

    @Override
    protected void initView()
    {
        setTitle(R.string.account_manager);
        setFinish();
        VirtualLayoutManager manager = new VirtualLayoutManager(this);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new DelegateAdapter(manager);
        ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        mRecyclerView.setAdapter(mAdapter);


        /*账号*/
        mAccountAdapter = new BaseDelegateAdapter<Integer>(this, R.layout.layout_account_manager)
        {
            @Override
            protected void convert(@Nullable BaseViewHolder holder, int position, @Nullable Integer integer)
            {
                holder.setCircleResource(R.id.img_account, R.mipmap.default_placehodler)
                        .setText(R.id.tv_account, R.string.tools_text)
                        .setVisibility(R.id.img_check, position == 0 ? View.VISIBLE : View.INVISIBLE)
                        .setVisibility(R.id.img_edit, isEditor ? View.VISIBLE : View.GONE)
                        .itemView.setOnClickListener(view ->
                {
                    if (mAccountDialog == null)
                    {
                        mAccountDialog = new DialogUtils(AccountManagerActivity.this)
                                .setTitle(R.string.whether_switch_accounts)
                                .setCanceledOnTouchOutside(false)
                                .setCancelable(true)
                                .setOnDialogClickListener(new DialogUtils.OnDialogClickListener()
                                {
                                    @Override
                                    public void setPositiveListener()
                                    {
                                        doFinish();
                                    }

                                    @Override
                                    public void setCancelLisntener()
                                    {
                                        alert("取消");
                                    }
                                });
                    }
                    mAccountDialog.show();
                });
            }
        };
        mAdapter.addAdapter(mAccountAdapter);
        mAccountAdapter.setNewData(Arrays.asList(new Integer[]{0, 0, 0, 0, 0, 0, 0, 0}));


        /*退出登录*/
        mLogoutAdapter = new BaseDelegateAdapter<Integer>(this, R.layout.layout_logout)
        {
            @Override
            protected void convert(@Nullable BaseViewHolder holder, int position, @Nullable Integer integer)
            {
                holder.itemView.setOnClickListener(view ->
                {
                    if (mLogoutDialog == null)
                    {
                        mLogoutDialog = new DialogUtils(AccountManagerActivity.this)
                                .setTitle(R.string.whether_logout)
                                .setCanceledOnTouchOutside(false)
                                .setCancelable(true)
                                .setOnDialogClickListener(new DialogUtils.OnDialogClickListener()
                                {
                                    @Override
                                    public void setPositiveListener()
                                    {
                                        alert("退出成功...");
                                    }

                                    @Override
                                    public void setCancelLisntener()
                                    {
                                        alert("取消");
                                    }
                                });
                    }
                    mLogoutDialog.show();
                });
            }

            @Override
            public LayoutHelper setLayoutHelper()
            {
                LinearLayoutHelper helper = new LinearLayoutHelper();
                helper.setBgColor(App.getResource().getColor(R.color.white));
                return helper;
            }
        };
        mAdapter.addAdapter(mLogoutAdapter);
        mLogoutAdapter.setNewData(1);
    }


    /*编辑按钮*/
    public void editor(View view)
    {

    }

    @Override
    protected void onDestroy()
    {
        if (mAccountDialog != null)
        {
            mAccountDialog.dismiss();
        }
        if (mLogoutDialog != null)
        {
            mLogoutDialog.dismiss();
        }
        super.onDestroy();
    }
}
